libConnect
	parent_type = /libBase


	var
		tblConnection = "core_Connections"

	Shutdown()
		CleanseConnections()


	proc
		CleanseConnections()
			Core.DB.runQuery("delete from `[tblConnection]`")


		connect(client/C)
			if(!(existsConnectionTable())) createConnectionTable()
			Core.DB.insertData("tbl=[getConnectionTable()];address=\"[C.address]\";bkey=\"[C.key]\";connected=\"[Core.TimeStamp()]\"")

		disconnect(client/C)
			if(!(existsConnectionTable())) createConnectionTable()
			Core.DB.runQuery({"update `[getConnectionTable()]`
			set disconnected = \"[Core.TimeStamp()]\"
			where address=\"[C.address]\" and bkey=\"[C.key]\" and disconnected is null"})

		createConnectionTable()
			if(!Core.DB.DBCon.IsConnected()) Core.DB.establishConnection()

			if(existsConnectionTable()) return FALSE

			var/sql = {"
			CREATE TABLE `[Core.DB.getDB()]`.`[getConnectionTable()]` (
				address VARCHAR(15) NOT NULL,
				bkey VARCHAR(255) NOT NULL,
				connected DATETIME NOT NULL,
				disconnected DATETIME
				) ENGINE=InnoDB;
			"}

			Core.DB.runQuery(sql)

			return existsConnectionTable()


		existsConnectionTable()
			var/DBQuery/q = Core.DB.Query("Describe [getConnectionTable()]")
			return q.Execute()

		setConnectionTable(tbl as text)
			tblConnection = tbl

		getConnectionTable()
			return tblConnection




client
	New()
		..()
		Core.Connect.connect(src)

	Del()
		Core.Connect.disconnect(src)
		Core.Mob.remMob("\ref[src.mob]")
		..()